package models

import (
	"public_security/plugins"
)

//其他Model的基类
type BaseModel struct {
	PerPage int64 `gorm:"-" json:"PerPage,omitempty"`
	Page    int64 `gorm:"-" json:"Page,omitempty"`
}

func (i *BaseModel) GetPageInfo() *plugins.ResultsPageInfo {
	pageInfo := new(plugins.ResultsPageInfo)
	if i.PerPage == 0 {
		i.PerPage = 15
	}

	pageInfo.PerPage = i.PerPage
	pageInfo.Page = i.Page

	return pageInfo
}

func (i *BaseModel) GetPageInfoNew() *plugins.ResultsPageInfo {
	pageInfo := new(plugins.ResultsPageInfo)
	if i.PerPage == 0 {
		i.PerPage = 15
	}
	pageInfo.PerPage = i.PerPage
	pageInfo.Page = i.Page
	if pageInfo.Total > 0 {
		pageInfo.TotalPage = pageInfo.Total / pageInfo.PerPage
		pageInfo.CurrentTotal = pageInfo.PerPage
		if supply := pageInfo.Total % pageInfo.PerPage; supply > 0 {
			pageInfo.TotalPage++
			if pageInfo.Page == pageInfo.TotalPage {
				pageInfo.CurrentTotal = supply
			}
		}
	}
	return pageInfo
}
